﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using MvcBHXH.Common;
using Newtonsoft.Json.Linq;

namespace MvcBHXH.Models
{
    public class MaBHYTTNModel
    {
        private DataClassesBHYTTNDataContext context;
        public MaBHYTTNModel() 
        {
            context = Util.CreateInstance().DataContext();
        }


        public List<MaBHYTTNModel> GetMaBHYTTNModel()
        {
            List<MaBHYTTNModel> listBHYTTN = new List<MaBHYTTNModel>();
            var BHYTTNModel = from fc in context.tbl_BHYTTNs
                              orderby fc.Ma_BHYTTN descending
                              select new 
                              {
                                fc.Ma_BHYTTN,
                                fc.Ma_DotBH,
                                fc.HoVaTen,
                                fc.NgaySinh,
                                fc.GioiTinh,
                                fc.DiaChi,
                                fc.Ma_CoSoKCB,
                                fc.TuNgay,
                                fc.DenNgay,
                                fc.Ma_LoaiBH,
                                fc.Ma_MucBH,
                                fc.NgayDangKy,
                                fc.NgayCapThe,
                                fc.TinhTrang,
                                fc.GhiChu,
                                fc.DonViBH_STT,
                                fc.MaSoThe,
                                fc.Ma_NSD,
                                fc.NgayTaoMoi,
                                fc.NgayCapNhat,
                              };
            MaBHYTTNModel MaBHYTTN;
            foreach (var item in BHYTTNModel)
            {
                MaBHYTTN = new MaBHYTTNModel();
                MaBHYTTN.MaBHYTTN = item.Ma_BHYTTN.ToString();
                MaBHYTTN.MaDotBH = item.Ma_DotBH.ToString();
                MaBHYTTN.HoVaTen = item.HoVaTen;
                MaBHYTTN.NgaySinh = item.NgaySinh;
                MaBHYTTN.GioiTinh = item.GioiTinh;
                MaBHYTTN.DiaChi = item.DiaChi;
                MaBHYTTN.CoSoKCB = item.Ma_CoSoKCB;
                MaBHYTTN.TuNgay = item.TuNgay;
                MaBHYTTN.DenNgay = item.DenNgay;
                MaBHYTTN.LoaiBH = item.Ma_LoaiBH;
                MaBHYTTN.MucBH = item.Ma_MucBH;
                //MaBHYTTN.NgayDangKy = (DateTime)item.NgayDangKy;
                //MaBHYTTN.NgayCapThe = (DateTime)item.NgayCapThe;
                MaBHYTTN.TinhTrang = item.TinhTrang;
                MaBHYTTN.GhiChu = item.GhiChu;
                MaBHYTTN.DonViBHSTT = item.DonViBH_STT;
                MaBHYTTN.MaSoThe = item.MaSoThe;
                MaBHYTTN.NguoiSudung = item.Ma_NSD;
                MaBHYTTN.NgayTaoMoi = item.NgayTaoMoi;
                //MaBHYTTN.NgayCapNhat = (DateTime)item.NgayCapNhat;
                listBHYTTN.Add(MaBHYTTN);
                
            }

            return listBHYTTN;
        }

        public List<MaBHYTTNModel> SearchBHYTTNModelByDot(string dotBH)
        {
            List<MaBHYTTNModel> listBHYTTN = new List<MaBHYTTNModel>();
            var BHYTTNModel = from fc in context.tbl_BHYTTNs
                              where fc.Ma_DotBH.Equals(dotBH)
                              orderby fc.Ma_BHYTTN descending
                              select new
                              {
                                  fc.Ma_BHYTTN,
                                  fc.Ma_DotBH,
                                  fc.HoVaTen,
                                  fc.NgaySinh,
                                  fc.GioiTinh,
                                  fc.DiaChi,
                                  fc.Ma_CoSoKCB,
                                  fc.TuNgay,
                                  fc.DenNgay,
                                  fc.Ma_LoaiBH,
                                  fc.Ma_MucBH,
                                  fc.NgayDangKy,
                                  fc.NgayCapThe,
                                  fc.TinhTrang,
                                  fc.GhiChu,
                                  fc.DonViBH_STT,
                                  fc.MaSoThe,
                                  fc.Ma_NSD,
                                  fc.NgayTaoMoi,
                                  fc.NgayCapNhat,
                              };
            MaBHYTTNModel MaBHYTTN;
            foreach (var item in BHYTTNModel)
            {
                MaBHYTTN = new MaBHYTTNModel();
                MaBHYTTN.MaBHYTTN = item.Ma_BHYTTN.ToString();
                MaBHYTTN.MaDotBH = item.Ma_DotBH.ToString();
                MaBHYTTN.HoVaTen = item.HoVaTen;
                MaBHYTTN.NgaySinh = item.NgaySinh;
                MaBHYTTN.GioiTinh = item.GioiTinh;
                MaBHYTTN.DiaChi = item.DiaChi;
                MaBHYTTN.CoSoKCB = item.Ma_CoSoKCB;
                MaBHYTTN.TuNgay = item.TuNgay;
                MaBHYTTN.DenNgay = item.DenNgay;
                MaBHYTTN.LoaiBH = item.Ma_LoaiBH;
                MaBHYTTN.MucBH = item.Ma_MucBH;
                //MaBHYTTN.NgayDangKy = (DateTime)item.NgayDangKy;
                //MaBHYTTN.NgayCapThe = (DateTime)item.NgayCapThe;
                MaBHYTTN.TinhTrang = item.TinhTrang;
                MaBHYTTN.GhiChu = item.GhiChu;
                MaBHYTTN.DonViBHSTT = item.DonViBH_STT;
                MaBHYTTN.MaSoThe = item.MaSoThe;
                MaBHYTTN.NguoiSudung = item.Ma_NSD;
                MaBHYTTN.NgayTaoMoi = item.NgayTaoMoi;
                //MaBHYTTN.NgayCapNhat = (DateTime)item.NgayCapNhat;
                listBHYTTN.Add(MaBHYTTN);

            }

            return listBHYTTN;
        }

        public List<MaBHYTTNModel> SearchBHYTTNModel(string maThe, string hoTen, string loaiBH, string dotBh, string mucBH)
        {
            List<MaBHYTTNModel> listBHYTTN = new List<MaBHYTTNModel>();
            Boolean hasLBH = false;
            Boolean hasDotBh = false;
            Boolean hasMucBH = false;
            if (loaiBH == "0")
            {
                hasLBH = true;
            }

            if (dotBh == "")
            {
                hasDotBh = true;
            }

            if (mucBH == null || mucBH == "0")
            {
                hasMucBH = true;
            }

                var BHYTTNModel = from fc in context.tbl_BHYTTNs
                                  where fc.MaSoThe.Contains(maThe) && fc.HoVaTen.Contains(hoTen) && (fc.Ma_DotBH.Equals(dotBh) || hasDotBh) && (fc.Ma_MucBH.Equals(mucBH) || hasMucBH) && (fc.Ma_LoaiBH.Equals(loaiBH) || hasLBH)
                                  orderby fc.Ma_BHYTTN descending
                        select new
                        {
                            fc.Ma_BHYTTN,
                            fc.Ma_DotBH,
                            fc.HoVaTen,
                            fc.NgaySinh,
                            fc.GioiTinh,
                            fc.DiaChi,
                            fc.Ma_CoSoKCB,
                            fc.TuNgay,
                            fc.DenNgay,
                            fc.Ma_LoaiBH,
                            fc.Ma_MucBH,
                            fc.NgayDangKy,
                            fc.NgayCapThe,
                            fc.TinhTrang,
                            fc.GhiChu,
                            fc.DonViBH_STT,
                            fc.MaSoThe,
                            fc.Ma_NSD,
                            fc.NgayTaoMoi,
                            fc.NgayCapNhat,
                        };
                MaBHYTTNModel MaBHYTTN;
                foreach (var item in BHYTTNModel)
                {
                    MaBHYTTN = new MaBHYTTNModel();
                    MaBHYTTN.MaBHYTTN = item.Ma_BHYTTN.ToString();
                    MaBHYTTN.MaDotBH = item.Ma_DotBH.ToString();
                    MaBHYTTN.HoVaTen = item.HoVaTen;
                    MaBHYTTN.NgaySinh = item.NgaySinh;
                    MaBHYTTN.GioiTinh = item.GioiTinh;
                    MaBHYTTN.DiaChi = item.DiaChi;
                    MaBHYTTN.CoSoKCB = item.Ma_CoSoKCB;
                    MaBHYTTN.TuNgay = item.TuNgay;
                    MaBHYTTN.DenNgay = item.DenNgay;
                    MaBHYTTN.LoaiBH = item.Ma_LoaiBH;
                    MaBHYTTN.MucBH = item.Ma_MucBH;
                    //MaBHYTTN.NgayDangKy = (DateTime)item.NgayDangKy;
                    //MaBHYTTN.NgayCapThe = (DateTime)item.NgayCapThe;
                    MaBHYTTN.TinhTrang = item.TinhTrang;
                    MaBHYTTN.GhiChu = item.GhiChu;
                    MaBHYTTN.DonViBHSTT = item.DonViBH_STT;
                    MaBHYTTN.MaSoThe = item.MaSoThe;
                    MaBHYTTN.NguoiSudung = item.Ma_NSD;
                    MaBHYTTN.NgayTaoMoi = item.NgayTaoMoi;
                    //MaBHYTTN.NgayCapNhat = (DateTime)item.NgayCapNhat;
                    listBHYTTN.Add(MaBHYTTN);

                }

                return listBHYTTN;
        }

        public MaBHYTTNModel GetMaBHYTTNModelById(string id)
        {
            var BHYTTNModel = from fc in context.tbl_BHYTTNs where fc.Ma_BHYTTN.Equals(id)
                              select new
                              {
                                  fc.Ma_BHYTTN,
                                  fc.Ma_DotBH,
                                  fc.HoVaTen,
                                  fc.NgaySinh,
                                  fc.GioiTinh,
                                  fc.DiaChi,
                                  fc.Ma_CoSoKCB,
                                  fc.TuNgay,
                                  fc.DenNgay,
                                  fc.Ma_LoaiBH,
                                  fc.Ma_MucBH,
                                  fc.NgayDangKy,
                                  fc.NgayCapThe,
                                  fc.TinhTrang,
                                  fc.GhiChu,
                                  fc.DonViBH_STT,
                                  fc.MaSoThe,
                                  fc.Ma_NSD,
                                  fc.NgayTaoMoi,
                                  fc.NgayCapNhat,
                              };
            MaBHYTTNModel MaBHYTTN;
            foreach (var item in BHYTTNModel)
            {
                MaBHYTTN = new MaBHYTTNModel();
                MaBHYTTN.MaBHYTTN = item.Ma_BHYTTN.ToString();
                MaBHYTTN.MaDotBH = item.Ma_DotBH.ToString();
                MaBHYTTN.HoVaTen = item.HoVaTen;
                MaBHYTTN.NgaySinh = item.NgaySinh;
                MaBHYTTN.GioiTinh = item.GioiTinh;
                MaBHYTTN.DiaChi = item.DiaChi;
                MaBHYTTN.CoSoKCB = item.Ma_CoSoKCB;
                MaBHYTTN.TuNgay = item.TuNgay;
                MaBHYTTN.DenNgay = item.DenNgay;
                MaBHYTTN.LoaiBH = item.Ma_LoaiBH;
                MaBHYTTN.MucBH = item.Ma_MucBH;
                //MaBHYTTN.NgayDangKy = (DateTime)item.NgayDangKy;
                //MaBHYTTN.NgayCapThe = (DateTime)item.NgayCapThe;
                MaBHYTTN.TinhTrang = item.TinhTrang;
                MaBHYTTN.GhiChu = item.GhiChu;
                MaBHYTTN.DonViBHSTT = item.DonViBH_STT;
                MaBHYTTN.MaSoThe = item.MaSoThe;
                MaBHYTTN.NguoiSudung = item.Ma_NSD;
                MaBHYTTN.NgayTaoMoi = item.NgayTaoMoi;
                //MaBHYTTN.NgayCapNhat = (DateTime)item.NgayCapNhat;
                return MaBHYTTN;

            }
            return new MaBHYTTNModel();
        }

        public Boolean Insert(string MaDotBH, string HoVaTen, string NgaySinh, string GioiTinh, string DiaChi, string CoSoKCB, string TuNgay, string DenNgay, string LoaiBH, string MucBH, string NSD, string GhiChu)
        {
            Log.Write("Class" + this.GetType().Name + " Start Insert() ");
            try
            {
                context.usp_BHYTTN_InsertOne(Int16.Parse(MaDotBH), HoVaTen, DateTime.Parse(NgaySinh), GioiTinh,
                DiaChi, CoSoKCB, DateTime.Parse(TuNgay), DateTime.Parse(DenNgay), LoaiBH, MucBH, GhiChu, NSD);
                context.SubmitChanges();
            }
            catch (Exception e)
            {
                Log.Write(" Insert Error ");
                Log.Write(e.Message.ToString());
                Log.Write("para : "+ Int16.Parse(MaDotBH) + " | " + HoVaTen + " | " + DateTime.Parse(NgaySinh) + " | " + GioiTinh + " | " + DiaChi + " | " + CoSoKCB + " | " + DateTime.Parse(TuNgay) + " | " + DateTime.Parse(DenNgay) + " | " + LoaiBH + " | " + MucBH + " | " + GhiChu + " | " + NSD);    
                return false;
            }
            Log.Write("para : " + Int16.Parse(MaDotBH) + " | " + HoVaTen + " | " + DateTime.Parse(NgaySinh) + " | " + GioiTinh + " | " + DiaChi + " | " + CoSoKCB + " | " + DateTime.Parse(TuNgay) + " | " + DateTime.Parse(DenNgay) + " | " + LoaiBH + " | " + MucBH + " | " + GhiChu + " | " + NSD);
            Log.Write("Class" + this.GetType().Name + " End Insert() ");
            return true;
        }

        public Boolean Update(string MaBHYTTN, string MaDotBH, string HoVaTen, string NgaySinh, string GioiTinh,
            string DiaChi, string CoSoKCB, string TuNgay, string DenNgay, string LoaiBH, string MucBH, string GhiChu, string NSD)
        {
            Log.Write("Class"+this.GetType().Name+".cs" + " Start Update() ");
            context.usp_BHYTTN_UpdateOne(Int16.Parse(MaBHYTTN), Int16.Parse(MaDotBH), HoVaTen, DateTime.Parse(NgaySinh), GioiTinh,
            DiaChi, CoSoKCB, DateTime.Parse(TuNgay), DateTime.Parse(DenNgay), LoaiBH, MucBH, GhiChu, NSD);
            Log.Write("EXEC [usp_BHYTTN_UpdateOne] '" + Int16.Parse(MaBHYTTN) + "','" + Int16.Parse(MaDotBH) + "','" + HoVaTen + "','" + DateTime.Parse(NgaySinh) + "','" + GioiTinh + "','" + DiaChi + "','" + CoSoKCB + "','" + DateTime.Parse(TuNgay) + "','" + DateTime.Parse(DenNgay) + "','" + LoaiBH + "','" + MucBH + "','" + GhiChu + "','" + NSD);
            context.SubmitChanges();
            Log.Write("Class" + this.GetType().Name + " End Update() ");
            return true;
        }

        public Boolean Delete(string id)
        {
            Log.Write("Class" + this.GetType().Name + " Start Delete() ");
            context.usp_BHXHYT_DeleteOne(Int16.Parse(id));
            context.SubmitChanges();
            Log.Write("Class" + this.GetType().Name + " End Delete() ");
            return true;
        }


        private string maBHYTTN;

        public string MaBHYTTN
        {
            get { return maBHYTTN; }
            set { maBHYTTN = value; }
        }

        private string maDotBH;

        public string MaDotBH
        {
            get { return maDotBH; }
            set { maDotBH = value; }
        }

        private string hoVaTen;

        public string HoVaTen
        {
            get { return hoVaTen; }
            set { hoVaTen = value; }
        }
        private DateTime ngaySinh;

        public DateTime NgaySinh
        {
            get { return ngaySinh; }
            set { ngaySinh = value; }
        }
        private string gioiTinh;

        public string GioiTinh
        {
            get { return gioiTinh; }
            set { gioiTinh = value; }
        }
        private string diaChi;

        public string DiaChi
        {
            get { return diaChi; }
            set { diaChi = value; }
        }
        private string coSoKCB;

        public string CoSoKCB
        {
            get { return coSoKCB; }
            set { coSoKCB = value; }
        }
        private DateTime tuNgay;

        public DateTime TuNgay
        {
            get { return tuNgay; }
            set { tuNgay = value; }
        }
        private DateTime denNgay;

        public DateTime DenNgay
        {
            get { return denNgay; }
            set { denNgay = value; }
        }
        private string loaiBH;

        public string LoaiBH
        {
            get { return loaiBH; }
            set { loaiBH = value; }
        }
        private string mucBH;

        public string MucBH
        {
            get { return mucBH; }
            set { mucBH = value; }
        }
        private DateTime ngayDangKy;

        public DateTime NgayDangKy
        {
            get { return ngayDangKy; }
            set { ngayDangKy = value; }
        }
        private DateTime ngayCapThe;

        public DateTime NgayCapThe
        {
            get { return ngayCapThe; }
            set { ngayCapThe = value; }
        }
        private int tinhTrang;

        public int TinhTrang
        {
            get { return tinhTrang; }
            set { tinhTrang = value; }
        }
        private string ghiChu;

        public string GhiChu
        {
            get { return ghiChu; }
            set { ghiChu = value; }
        }
        private int donViBHSTT;

        public int DonViBHSTT
        {
            get { return donViBHSTT; }
            set { donViBHSTT = value; }
        }
        private string maSoThe;

        public string MaSoThe
        {
            get { return maSoThe; }
            set { maSoThe = value; }
        }
        private string nguoiSudung;

        public string NguoiSudung
        {
            get { return nguoiSudung; }
            set { nguoiSudung = value; }
        }
        private DateTime ngayTaoMoi;

        public DateTime NgayTaoMoi
        {
            get { return ngayTaoMoi; }
            set { ngayTaoMoi = value; }
        }
        private DateTime ngayCapNhat;

        public DateTime NgayCapNhat
        {
            get { return ngayCapNhat; }
            set { ngayCapNhat = value; }
        }
    }
}